package pkg.hicph.org;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;


@SuppressWarnings("rawtypes")
public class PlaceOverlayManager extends ItemizedOverlay {

	// Preference file
//    public static final String PREF_FILE = "pf_file";
//    private SharedPreferences preferences;
    
	// list of overlays
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext; 
	
	public PlaceOverlayManager(Drawable defaultMarker) {
		super(boundCenter(defaultMarker));
		//super(defaultMarker);
	}
	
	public PlaceOverlayManager(Drawable defaultMarker, Context context) {
		super(boundCenter(defaultMarker));
		mContext = context;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	public void populateNow(){
		populate();
	}

	// method for add new overlays
	public void addOverlay(OverlayItem overlay) {
	    
		mOverlays.add(overlay);
		
	    // Each time you add a new OverlayItem to the ArrayList, 
    	// you must call populate() for the ItemizedOverlay, 
    	// which will read each of the OverlayItem
    	// When the populate() method executes, it will call createItem(int) 
    	// in the ItemizedOverlay to retrieve each OverlayItem. 

		populate();
	    
	}
	
	protected int getIndexToDraw(int drawingOrder){

	    return super.getIndexToDraw(drawingOrder);
	}
	
	public int removeOverlay(OverlayItem overlay) {
	    
	    mOverlays.remove(overlay);
	    //populate();
	    
	    return mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
		
		Log.d("TAP_I_START",index+"");
		Log.d("TAP_DIALOG","ENTERED");
		
		OverlayItem item = mOverlays.get(index);
		
		Log.d("TAP_SNIPPET", item.getSnippet());
		
		int indexPlaces = -1;
		
		if (item.getSnippet() != "player"){
			indexPlaces = new Integer(item.getSnippet());
		
		
			Log.d("TAP_INDEX", indexPlaces+"");
			
//			Intent newGameIntent = new Intent(mContext, PlaceDialog.class);
//    		mContext.startActivity(newGameIntent);
		
//			if (Utils.globalPlaces[indexPlaces].clickable){
//				AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
//				dialog.setTitle(item.getTitle());
//				dialog.setMessage(item.getSnippet());
//				dialog.show();
//				return true;
//			}
			
//			Log.d("TAP DIST", Utils.globalPlaces[indexPlaces].dist + "");
			
//			if (Utils.globalPlaces[indexPlaces].dist >= 40){
//				Utils.showToastShort(mContext, "Place is too far!");
//			}
//			else if(Utils.globalPlaces[indexPlaces].clickable){
//				
////				Log.d("CLICK","" + Utils.globalPlaces[indexPlaces].action);
//				
//				Utils.activePlace = Utils.globalPlaces[indexPlaces];
//				
//				Intent newGameIntent = new Intent(mContext, PlaceDialog.class);
//	    		mContext.startActivity(newGameIntent);
//				
////				Utils.showToastShort(mContext, Utils.globalPlaces[indexPlaces].name + "\n" + Utils.globalPlaces[indexPlaces].action);
//			}
			
			return true;
			
		}
		return false;
			
		
		
	}
	
}

